package com.bzsg.simpleMediaPlayer.ui.pager

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.bzsg.simpleMediaPlayer.learn.CounterViewModel
import com.bzsg.simpleMediaPlayer.ui.pager.thumbnail.VideoGalleryScreen
import com.bzsg.simpleMediaPlayer.viewModel.MainViewModel

// 4. 主界面组合
@Composable
fun MainScreen(viewModel: MainViewModel = hiltViewModel()) {
    val navController = rememberNavController()

    // 使用更简洁的导航状态管理
    val currentRoute by navController.currentBackStackEntryAsState()
    val currentDestination = currentRoute?.destination?.route

    // 自动同步导航状态到ViewModel
    LaunchedEffect(currentDestination) {
        currentDestination?.let { route ->
            when (route) {
                Screen.Home.route -> viewModel.updateSelectedNavItem(Screen.Home.route)
                Screen.Search.route -> viewModel.updateSelectedNavItem(Screen.Search.route)
                Screen.Profile.route -> viewModel.updateSelectedNavItem(Screen.Profile.route)
            }
        }
    }

    Scaffold(
        bottomBar = {
            NavigationBar {
                listOf(Screen.Home, Screen.Search, Screen.Profile).forEach { screen ->
                    NavigationBarItem(
                        icon = { screen.icon() },
                        label = { Text(screen.title) },
                        selected = viewModel.selectedNavItem == screen.route,
                        onClick = {
                            if (viewModel.selectedNavItem != screen.route) {
                                navController.navigate(screen.route) {
                                    popUpTo(navController.graph.findStartDestination().id) {
                                        saveState = true
                                    }
                                    launchSingleTop = true
                                    restoreState = true
                                }
                            }
                        },
                    )
                }
            }
        },
    ) { innerPadding ->
        NavHost(
            navController = navController,
            startDestination = Screen.Home.route,
            modifier = Modifier.padding(innerPadding),
        ) {
            composable(Screen.Home.route) { HomeScreen() }
            composable(Screen.Search.route) { SearchScreen() }
            composable(Screen.Profile.route) { ProfileScreen() }
        }
    }
}

// 4. 各页面组件
@Composable
fun HomeScreen(viewModel: CounterViewModel = hiltViewModel()) {
    PermissionRequest()
    VideoGalleryScreen()
}

@Composable
fun SearchScreen() {
    Box(
        modifier =
            Modifier
                .fillMaxSize()
                .background(Color(0xFFE8F5E9)),
        // 浅绿色
        contentAlignment = Alignment.Center,
    ) {
        Text("搜索页面\n\n左右滑动切换页面", fontSize = 24.sp)
    }
}

@Composable
fun ProfileScreen() {
    Box(
        modifier =
            Modifier
                .fillMaxSize()
                .background(Color(0xFFF3E5F5)),
        // 浅紫色
        contentAlignment = Alignment.Center,
    ) {
        Text("个人资料\n\n左右滑动切换页面", fontSize = 24.sp)
    }
}
